package com.yongninger.activity.product;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yongninger.activity.R;
import com.yongninger.dao.ProductDao;
import com.yongninger.vo.ProductVo;

public class ViewActivity extends Activity {
	private ProductDao ProductDao;
	ProductVo productVo;
	EditText pname;
	EditText pcode;
	EditText pcatetory;
	EditText ptype;
	EditText pprice;;
	EditText smeasure1;;
	EditText smeasure2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_view);
		ProductDao = new ProductDao(getApplicationContext());
		Intent intent = getIntent();
		productVo = (ProductVo) intent
				.getSerializableExtra("productVo");
		pname = (EditText)findViewById(R.id.pname);
		pname.setText(productVo.getPname());
		pcode = (EditText)findViewById(R.id.pcode);
		pcode.setText(productVo.getPcode());
		pcatetory = (EditText)findViewById(R.id.pcategory);
		pcatetory.setText(productVo.getPcategory());
		ptype = (EditText)findViewById(R.id.ptype);
		ptype.setText(productVo.getPtype());
		pprice = (EditText)findViewById(R.id.pprice);
		pprice.setText(productVo.getPprice());
		smeasure1 = (EditText)findViewById(R.id.smeasure1);
		smeasure1.setText(productVo.getSmeasure1());
		smeasure2 = (EditText)findViewById(R.id.smeasure2);
		smeasure2.setText(productVo.getSmeasure2());
		Button modifyBtn = (Button) this.findViewById(R.id.modify);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (update()) {
					Toast.makeText(getApplicationContext(), "修改成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "修改失败", 2).show();
				}
			}
		};
		modifyBtn.setOnClickListener(onClickListener);

		/*
		 * 删除按钮
		 */
		Button deleteBtn = (Button) this.findViewById(R.id.delete);
		OnClickListener onClickListener1 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 删除
				if(ProductDao.beHaveShipsment(productVo.getPname())){
					boolean result = ProductDao.deleteProductVo(productVo.getPid());
					if (result) {
						Toast.makeText(getApplicationContext(), "删除成功", 2).show();
						// 跳转到查询页面
						Intent intent = new Intent(ViewActivity.this,
								ListActivity.class);
						startActivity(intent);
					} else {
						Toast.makeText(getApplicationContext(), "删除失败", 2).show();
					}
				}else{
					Toast.makeText(getApplicationContext(), "单据存在此产品资料,不能删除", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				}
				
			}
		};
		deleteBtn.setOnClickListener(onClickListener1);
	}

	private boolean update() {
		ProductVo productVoVoTemp = new ProductVo();
		productVoVoTemp.setPcategory(pcatetory.getText().toString());
		productVoVoTemp.setPcode(pcode.getText().toString());
		productVoVoTemp.setPname(pname.getText().toString());
		productVoVoTemp.setPprice(pprice.getText().toString());
		productVoVoTemp.setPtype(ptype.getText().toString());
		productVoVoTemp.setSmeasure1(smeasure1.getText().toString());
		productVoVoTemp.setSmeasure2(smeasure2.getText().toString());
		return ProductDao.updateProductVo(productVoVoTemp, productVo.getPid());
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Toast.makeText(getApplicationContext(), "请先保存内容",
					Toast.LENGTH_SHORT).show();
			return false;
		}else if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() >= 0){
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(ViewActivity.this,
					ListActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
